import { View, Text } from '@tarojs/components';
import './index.scss';

interface IProps {
  price: string | number;
  // 是否除以100，只对number有效
  flag?: boolean;
  color?: string;
}

export default function ({ price, flag, color = '#f82256' }: IProps) {
  let priceFirst = '';
  let priceLast = '';
  if (typeof price !== 'string' && typeof price !== 'number') {
    return null;
  }
  if (typeof price === 'string') {
    const priceList = price.split('.');
    priceFirst = priceList[0];
    priceLast = priceList[1];
  }
  if (typeof price === 'number') {
    let tempPrice = price;
    if (flag) {
      tempPrice = price / 100;
    }
    const priceStr = tempPrice.toFixed(2);
    const priceList = priceStr.split('.');
    priceFirst = priceList[0];
    priceLast = priceList[1];
  }
  return (
    <View className="price-component" style={{ color }}>
      <Text className="currency">￥</Text>
      <Text className="price-first">{priceFirst}</Text>
      <Text className="price-last">.{priceLast}</Text>
    </View>
  );
}
